package executors

import db.Folder
import org.hibernate.{Session, SessionFactory}
import org.hibernate.cfg.Configuration
import db.FolderManager
import db.Client
import client_updater.{ Update, ClientsUpdater }
import client_updater.FolderUpdate
import scala.collection.mutable.LinkedList
import objects.File1
import scala.actors.Actor

/**
 * Processes an update about a folder that was created in a client's dropbox.<br>
 * communicates with the database and sends updates for other clients.
 */
class NewFolderExecutor extends Actor {

  /**
   * the acting method of the actor
   */
  def act(){
    loop{
      react{
        case (receivedFolder : File1)=> 
          sender ! execute(receivedFolder)
        case "Quit" => exit()
        case _ => println("NewFolderExecutor received unknown message")
      }
    }
  }
  
  
  /**
   * receives a File1 that contains details about a folder that was created in a client's dropbox.<br>
   * communicates with the database and the disk, and sends updates for other clients.<br>
   * @param - receivedFolder : File1, details about the received folder<br>
   * @returns the ID of the folder.
   */
  private def execute(receivedFolder: File1): Long = {
    var folder = new Folder
    folder.setFolderID(0)
    val cfg = new Configuration()
    var sessionFactory = null.asInstanceOf[SessionFactory]
    var session = null.asInstanceOf[Session]
    try{
    sessionFactory = cfg.configure().buildSessionFactory()
    session = sessionFactory.openSession()
    var query = session.createQuery("from Folder.folderID")
    val list = query.list.toArray.asInstanceOf[Array[Long]]
    var max = list.max    
    folder.setFolderID(max+1)
    folder.setUserID(receivedFolder.uid)
    folder.setIsAdmin(new java.lang.Integer(1))
    folder.setFolderName(receivedFolder.name)
    folder.setParentFolder(receivedFolder.parent)
    val folderManager = new FolderManager(session)
    folderManager.saveFolder(folder)
    session.flush
    /*var query = session.createQuery("from folders where user_ID = '" +
      folder.getUserID + "' and folder_name = '" +
      folder.getFolderName + "' and parent = '" +
      folder.parent + "'")
    folder = query.list.get(0).asInstanceOf[Folder]
    */
    query = session.createQuery("from Client c where c.userID = " + receivedFolder.uid)
    var clients = query.list().toArray
    for( c <- clients ){
      if(c.asInstanceOf[Client].getClientID != receivedFolder.cid)
      ClientsUpdater.addUpdate(c.asInstanceOf[Client].getClientID, 
    		  				new FolderUpdate("newFolder", receivedFolder.name, 
    		  				receivedFolder.parent, folder.getFolderID))
    }
    /*list.foreach(c => {
      var update = new FolderUpdate("NewFolder", receivedFolder.name, receivedFolder.parent, folder.getFolderID)
      ClientsUpdater.addUpdate(c, update)
    })*/
    session.flush
    }catch{ case (e : Exception) => println(e.printStackTrace) }
    return folder.getFolderID
  }
/*
  private def init(){
    val cfg = new Configuration()
    var sessionFactory = null.asInstanceOf[SessionFactory]
    var session = null.asInstanceOf[Session]
    try{
    sessionFactory = cfg.configure().buildSessionFactory()
    session = sessionFactory.openSession()
    val query = session.createQuery("from Folder.folderID")
    val list = query.list.toArray.asInstanceOf[Array[Long]]
    var max = list.max
    count = max
    } catch { case (e : Exception) => e.printStackTrace }
  }
*/
}